/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.io;

import java.io.OutputStream;
import ca.forklabs.baselib.io.Resources;

import java.io.IOException;

/**
 * Class {@code AbstractOutputStream} provides common functionality for output
 * streams.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.io.AbstractOutputStream">Daniel Léonard</a>
 * @version $Revision$
 */
public abstract class AbstractOutputStream extends OutputStream {

//---------------------------
// Instance variables
//---------------------------

   /** Determines if the stream is opened or closed. */
   private boolean is_closed = false;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    */
   protected AbstractOutputStream() {
   // nothing
      }


//---------------------------
// Accessors and mutators
//---------------------------

    /**
     * Determines if the writer is closed.
     * @return   {@code true} if the writer is closed, {@code false} otherwise.
     */
    protected boolean isClosed() {
       return this.is_closed;
       }


//---------------------------
// Overridden methods from java.io.InputStream
//---------------------------

    /**
     * Closes this stream.
     */
    @Override
    public void close() {
       this.is_closed = true;
       }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Ensures that the stream is opened.
    * @exception   IOException   if the stream is closed.
    */
   protected void ensureStreamOpened() throws IOException {
      boolean is_stream_closed = this.isClosed();
      if (is_stream_closed) {
         String message = this.getStreamIsClosedErrorMessage();
         throw new IOException(message);
         }
      }

   /**
    * Gets the formatted error message about the fact that the writer is closed.
    * @return   the formatted error message.
    */
   protected String getStreamIsClosedErrorMessage() {
      String key = Resources.OUTPUT_STREAM_IS_CLOSED;
      String message = Resources.getLocalizedString(key);
      return message;
      }

   }
